package com.example.moviegroup.fragment;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.moviegroup.R;
import com.example.moviegroup.adapter.MyGridViewAdapter;
import com.example.moviegroup.adapter.MyPagerAdapter;
import com.example.moviegroup.app.Logger;
import com.example.moviegroup.bean.FilmBean;
import com.example.moviegroup.utils.MyUrls;
import com.google.gson.Gson;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;

public class MovieFragment extends Fragment {
	private TextView tv_theme;
	private TextView tv_mv_name;
	private TextView tv_mv_content;
	private TextView tv_mv_time;
	private TextView tv_dianzan_num;
	private TextView tv_guanzhu_num;
	private ImageView iv_movie_image;
	private ImageView iv_dianzan;
	private ImageView iv_guanzhu;
	private ViewPager pager;
	private MyPagerAdapter adapter;
	private View v;
	private ArrayList<FilmBean.Film> list =new ArrayList<FilmBean.Film>();
	private RelativeLayout layout;
	private RelativeLayout afater;
	private LinearLayout gridlist;
	private HttpUtils utils;
	private GridView gridView;
	private EditText editText;
	private boolean flag;
	private MyGridViewAdapter gridViewAdapter;
	private BitmapUtils bitmapUtils;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		initView();
		setListener();
		initData();
		return v;
	}

	private void initView() {
		 v=View.inflate(getActivity(),R.layout.fragment_movie,null);
		tv_theme= (TextView) v.findViewById(R.id.tv_theme);
		tv_guanzhu_num= (TextView) v.findViewById(R.id.tv_guanzhu_num);
		tv_mv_name= (TextView) v.findViewById(R.id.tv_mv_name);
		tv_mv_content= (TextView) v.findViewById(R.id.tv_mv_tv1);
		tv_mv_time= (TextView) v.findViewById(R.id.tv_mv_time);
		tv_dianzan_num= (TextView) v.findViewById(R.id.tv_num);
		iv_dianzan=(ImageView)v.findViewById(R.id.iv_dianzan);
		iv_guanzhu=(ImageView)v.findViewById(R.id.iv_guanzhu);
		iv_movie_image= (ImageView) v.findViewById(R.id.iv_movie_bg);
		layout= (RelativeLayout) v.findViewById(R.id.movielayout);
		gridlist= (LinearLayout) v.findViewById(R.id.gridlist);
		gridView= (GridView) v.findViewById(R.id.gd_image);
		editText= (EditText) v.findViewById(R.id.et_search);
		afater= (RelativeLayout) v.findViewById(R.id.Relative_after);
		pager= (ViewPager) v.findViewById(R.id.pager);
		adapter=new MyPagerAdapter(getActivity());
		gridViewAdapter=new MyGridViewAdapter(getActivity());
		adapter.setList(list);
		pager.setPageMargin(-200);
		pager.setOffscreenPageLimit(3);
		pager.setAdapter(adapter);
		utils=new HttpUtils();

		bitmapUtils=new BitmapUtils(getActivity());
		//if(IApplication.isNetworkAvailable(getActivity())){
			utils.send(HttpRequest.HttpMethod.GET, MyUrls.MV_JSON_URL, new RequestCallBack<String>() {
				@Override
				public void onSuccess(ResponseInfo<String> responseInfo) {
					Gson gson=new Gson();
					FilmBean bean=gson.fromJson( responseInfo.result,FilmBean.class);
						list.addAll(bean.films);
					adapter.setList(list);
					adapter.notifyDataSetChanged();
					FilmBean.Film film=list.get(0);
					tv_dianzan_num.setText(film.praise_count);
					tv_guanzhu_num.setText(film.follow_count);
					tv_mv_name.setText("《"+film.film_name+"》");
					tv_mv_content.setText(film.slogan);
					tv_mv_time.setText(film.release_date + "上映，片长" + film.film_longer + "分钟");
					bitmapUtils.display(iv_movie_image, MyUrls.MV_BASE + list.get(0).photo_url, new BitmapLoadCallBack<ImageView>() {
						@Override
						public void onLoadCompleted(ImageView imageView, String s, Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig, BitmapLoadFrom bitmapLoadFrom) {
							Bitmap bit = BitmapFactory.decodeResource(getResources(), R.mipmap.guide1);
							ByteArrayOutputStream baos = new ByteArrayOutputStream();
							bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
							iv_movie_image.setImageBitmap(blurBitmap(Bytes2Bimap(baos.toByteArray()), 15, getActivity()));
						}

						@Override
						public void onLoadFailed(ImageView imageView, String s, Drawable drawable) {

						}
					});
				}

				@Override
				public void onFailure(HttpException e, String s) {
					Logger.d(s+"网络请求出错");
				}
			});
//		}else{
//			Logger.d("网络兜风去了！");
//		}

	}
	private void setListener() {
		pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

			}

			@Override
			public void onPageSelected(int position) {
				FilmBean.Film film=list.get(position);
					tv_dianzan_num.setText(film.praise_count);
					tv_guanzhu_num.setText(film.follow_count);
					tv_mv_name.setText("《"+film.film_name+"》");
					tv_mv_content.setText(film.slogan);
					tv_mv_time.setText(film.release_date + "上映，片长" + film.film_longer + "分钟");
					bitmapUtils.display(iv_movie_image, MyUrls.MV_BASE + list.get(position).photo_url, new BitmapLoadCallBack<ImageView>() {
					@Override
					public void onLoadCompleted(ImageView imageView, String s, Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig, BitmapLoadFrom bitmapLoadFrom) {
						Bitmap bit=BitmapFactory.decodeResource(getResources(), R.mipmap.guide1);
						ByteArrayOutputStream baos = new ByteArrayOutputStream();
						bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
						iv_movie_image.setImageBitmap(blurBitmap(Bytes2Bimap(baos.toByteArray()),15,getActivity()));
					}

					@Override
					public void onLoadFailed(ImageView imageView, String s, Drawable drawable) {

					}
				});
			}

			@Override
			public void onPageScrollStateChanged(int state) {

			}
		});
		tv_theme.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if(flag){
					tv_theme.setText(R.string.movie_list);
					pager.setVisibility(View.VISIBLE);
					afater.setVisibility(View.VISIBLE);
					gridlist.setVisibility(View.GONE);
					flag=false;
				}else{
					tv_theme.setText(R.string.movie_theme);
					gridViewAdapter.setList(list);
					gridView.setAdapter(gridViewAdapter);
					pager.setVisibility(View.GONE);
					afater.setVisibility(View.GONE);
					gridlist.setVisibility(View.VISIBLE);
					flag=true;
				}

			}
		});
	}

	@Override
	public void onResume() {
		super.onResume();
		if(flag){
			tv_theme.setText(R.string.movie_theme);
			gridViewAdapter.setList(list);
			gridView.setAdapter(gridViewAdapter);
			pager.setVisibility(View.GONE);
			afater.setVisibility(View.GONE);
			gridlist.setVisibility(View.VISIBLE);
		}else{
			tv_theme.setText(R.string.movie_list);
			pager.setVisibility(View.VISIBLE);
			afater.setVisibility(View.VISIBLE);
			gridlist.setVisibility(View.GONE);
		}
	}

	private void initData() {

	}
	public static Bitmap blurBitmap(Bitmap bitmap, float radius, Context context) {

		// Let's create an empty bitmap with the same size of the bitmap we want
		// to blur
		Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(),
				bitmap.getHeight(), Bitmap.Config.ARGB_8888);

		// Instantiate a new Renderscript
		RenderScript rs = RenderScript.create(context);

		// Create an Intrinsic Blur Script using the Renderscript
		ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs,
				Element.U8_4(rs));

		// Create the Allocations (in/out) with the Renderscript and the in/out
		// bitmaps
		Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
		Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);

		// Set the radius of the blur
		if (radius > 25) {
			radius = 25.0f;
		} else if (radius <= 0) {
			radius = 1.0f;
		}
		blurScript.setRadius(radius);

		// Perform the Renderscript
		blurScript.setInput(allIn);
		blurScript.forEach(allOut);

		// Copy the final bitmap created by the out Allocation to the outBitmap
		allOut.copyTo(outBitmap);

		// recycle the original bitmap
		bitmap.recycle();
		bitmap = null;
		// After finishing everything, we destroy the Renderscript.
		rs.destroy();

		return outBitmap;
	}
	public Bitmap Bytes2Bimap(byte[] b){
		if(b.length!=0){
			return BitmapFactory.decodeByteArray(b, 0, b.length);
		}
		else {
			return null;
		}
	}
}
